from flask import Flask, request, render_template

webapp = Flask(__name__)


# 表单必须放在templates一个文件夹里，webapp与文件夹同一目录
# get post 请求
@webapp.route('/', methods=['GET', 'POST'])
def home():
    return render_template('home.html')


# 登录表单---get请求
@webapp.route('/login', methods=['GET'])
def login_form():
    return render_template('form.html')


# post 带表单
@webapp.route('/login', methods=['POST'])
def login():
    username = request.form['username']
    password = request.form['password']
    if username == 'admin' and password == '123456':
        return render_template('login-ok.html', username=username)
    return render_template('form.html', message='bad username or password', username=username)


if __name__ == '__main__':
    # 若不配置host和port，则默认是localhost，端口为5000
    # 若配置，如写作app.run("",8000)，就是localhost，端口8000
    webapp.run("", 8080)
